<?php
namespace Home\Controller;
use Think\Controller;
class FoodController extends AuthController{
	/**
	 * 获取食物标签
	 */
	public function getFoodTag(){
		$list = D('FoodTag')->order("FoodType.index desc,FoodTag.food_type desc")->select();
		$type = "";
		$tags = array();
		foreach($list as $item){
			if($type != $item['type']){
				$type = $item['type'];
			}
			$tag = array();
			$tag['tag'] = $item['tag'];
			$tag['id'] = $item['id'];
			$tags[$type][] = $tag;
		}
		$this->assign('tags',$tags);
	}
	/**
	 * 获取该食品的类别
	 */
	public function category(){
		$where['fid'] = I("fid");
		$list = M("FoodCategory")->where($where)->select();
		if($list){
// 			$result = M("FoodCategory")->where($where)->field("min(price) as sprice,max(price) as lprice")->select();
// 			$json = $result[0];
			$json['result'] = 1;
			$json['list'] = $list;
		}else{
			$json['result'] = 0;
		}
		$this->ajaxReturn($json);
	}
	/**
	 * 获取食品列表
	 */
	public function index(){
		$this->getFoodTag();
		$pageSize = 32;
		$Food = D('FoodView'); // 实例化User对象
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		$list = $Food->where(array("Food.type"=>"蔬菜"))->group('Food.id')->order('Food.ctime')->page($_GET['p'],$pageSize)->select();
		$this->assign('list',$list);// 赋值数据集
		$count      = $Food->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,$pageSize);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig("next","下一页");
		$Page->setConfig("prev","上一页");
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('totalPages',$Page->totalPages);// 总页数
		$this->display(); // 输出模板
	}
	public function search(){
		$this->getFoodTag();
		$pageSize = 32;
		$search = I("search");
		$where['food_name'] = array("LIKE","%{$search}%");
		$Food = D('FoodView');  // 实例化User对象
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		$list = $Food->group('Food.id')->where($where)->order('ctime')->page($_GET['p'],$pageSize)->select();
		$this->assign('list',$list);// 赋值数据集
		$count      = $Food->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,$pageSize);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig("next","下一页");
		$Page->setConfig("prev","上一页");
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('totalPages',$Page->totalPages);// 总页数
		$this->display(); // 输出模板
	}
	public function getFoodByTag(){
		$this->getFoodTag();
		$where['id'] = I('tag');
		$tag = M('FoodTag')->field("tag_name as tag,food_type as type")->where($where)->find();
		$where2['food_tag'] = $tag['tag'];
		$where2['type'] = $tag['type'];
		$pageSize = 32;
		$Food = D('FoodView');  // 实例化User对象
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		$list = $Food->group('Food.id')->where($where2)->order('ctime')->page($_GET['p'],$pageSize)->select();
		$this->assign('list',$list);// 赋值数据集
		$count      = $Food->where($where2)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,$pageSize);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig("next","下一页");
		$Page->setConfig("prev","上一页");
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('totalPages',$Page->totalPages);// 总页数
		$this->display('tag'); // 输出模板
	}
}